function getInfo() {
	ob_start();
	$o = phpinfo();
	file_put_contents("/Users/john/f",ob_get_clean());
	return ob_get_clean();
}

function getUserName() {
	$home = getenv("HOME");
	$f = file($home."/.cryptochat/keystore/userconfig.ini");
	foreach($f as $ff) {
		if (strstr($ff,"username=")) {
			return(trim(str_replace("username=","",$ff)));
		}
	}
}

function encryptServerVar($cwd,$message) {
	$home = getenv("HOME");
	//get the server pubkey
	$public_key_str = file_get_contents($home."/.cryptochat/keystore/server.pem");
	$public_key = openssl_pkey_get_public($public_key_str);

	$a_envelope = array();
	$a_key = array($public_key);
	
	openssl_seal($message,$sealed,$a_envelope,$a_key);
	openssl_free_key($public_key);	
	


	$bsealed = base64_encode($sealed);
	$benvelope = base64_encode($a_envelope[0]);
	$bsealed=urlencode($bsealed);
	$benvelope=urlencode($benvelope);
	$vars = array($bsealed,$benvelope);
	openssl_free_key($public_key);
	return $vars;
}

function sendMessage($cwd,$servermessage,$serveruser="",$user="",$usermessage="") {
	$home = getenv("HOME");
	$servermessage_envelope = "";
	$serveruser_envelope = "";
	$user_envelope = "";
	$usermessage_envelope = "";
	if (trim($servermessage) == "") {
		return;
	} else {
		$vars = encryptServerVar($cwd,$servermessage);
		$servermessage=$vars[0];
		$servermessage_envelope=$vars[1];
	}
	
	$uu = getUsername();
	$from = encryptServerVar($cwd,$uu);
	$fromuser = $from[0];
	$fromuser_envelope=$from[1];
	
	$fields=array(
		'fromuser' => $fromuser,
		'fromuser_envelope' => $fromuser_envelope,
		'servermessage' => $servermessage,
		'servermessage_envelope'=> $servermessage_envelope,
		'serveruser' => $serveruser,
		'serveruser_envelope'=> $serveruser_envelope,
		'user' => $user,
		'user_envelope'=> $user_envelope,
		'usermessage' => $usermessage,
		'usermessage_envelope'=> $usermessage_envelope
	);

	foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
	rtrim($fields_string, '&');
	
	$url = 'https://www.sipmeeting.com/cryptochat/messages.php';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, count($fields));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	curl_setopt($ch, CURLOPT_CAPATH,"\"".$cwd."/Resources/ca\"");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSLVERSION, 3);
	$result = curl_exec($ch);
	curl_close($ch);
	error_log($cwd);	
	
	
}

function generateKeys($cwd) {
	$home = getenv("HOME");
	//$home="/Users/john";
	if (!file_exists($home."/.cryptochat/keystore")) {
		mkdir($home."/.cryptochat/keystore",0777,true);
	}

	if (!file_exists($home."/.cryptochat/keystore/mypriv.key") || !file_exists($home."/.cryptochat/keystore/mypub.key")) {
		$config = array(
			"digest_alg" => "sha512",
			"private_key_bits" => 4096,
			"private_key_type" => OPENSSL_KEYTYPE_RSA,
		);

		// Create the keypair
		$res=openssl_pkey_new($config);

		// Get private key
		openssl_pkey_export($res, $privkey);

		// Get public key
		$pubkey=openssl_pkey_get_details($res);
		file_put_contents($home."/.cryptochat/keystore/mypriv.key",$privkey);
		file_put_contents($home."/.cryptochat/keystore/mypub.key",$pubkey["key"]);

	}
	if (!file_exists($home."/.cryptochat/keystore/server.pem")) {
		$uu = getUserName();
		file_put_contents($home."/.cryptochat/keystore/server.pem",file_get_contents("https://www.sipmeeting.com/pubkey.pem"));
		//get my pub key and encrypt it
		/*change this to upload the file instead...*/
		
		$key = getPublicKey();
		$mykey=file_get_contents($key);
		

		$public_key_str = file_get_contents($home."/.cryptochat/keystore/server.pem");
		$public_key = openssl_pkey_get_public($public_key_str);

		$a_envelope = array();
		$a_key = array($public_key);
		openssl_seal($mykey,$sealed,$a_envelope,$a_key);
		openssl_free_key($public_key);		
		
		$bsealed = base64_encode($sealed);
		$benvelope = base64_encode($a_envelope[0]);
		$bsealed=urlencode($bsealed);
		$benvelope=urlencode($benvelope);
		
		$fields=array(
			'message' => $bsealed,
			'envelope'=> $benvelope,
			'from_user' => $uu
		);

		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
		rtrim($fields_string, '&');
		
		$url = 'https://www.sipmeeting.com/cryptochat/givekey.php';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, count($fields));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_CAPATH,"\"".$cwd."/Resources/ca\"");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSLVERSION, 3);
		$result = curl_exec($ch);
		curl_close($ch);
		error_log($cwd);

	}
	if (!file_exists($home."/.cryptochat/keystore/userconfig.ini")) {
		$uu=uniqid(microtime(true),true);
		//store the useraccount
		file_put_contents($home."/.cryptochat/keystore/userconfig.ini","username=$uu\n");
	}
}

function getPublicKey() {
	$home = getenv("HOME");
	//$home="/Users/john";
	$key = $home."/.cryptochat/keystore/mypub.key";
	return $key;
	//$home."/.cryptochat/keystore/mypub.key"
	
}
